﻿using Assets.Script.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Assets.Script.Human.Player.Action
{
    public class BeAtkAction : BasePlayerAction, IAction
    {
        float time = 0;
        Vector3 offSetDir;
        float offSetDistance;
        float offSetTime;
        float offSetSpeed;
        private AudioSource audioSource;

        public BeAtkAction(PlayerBody playerBody) : base(playerBody)
        {
            audioSource = playerBody.gameObject.AddComponent<AudioSource>();
            audioSource.playOnAwake = false;
            audioSource.clip = Resources.Load<AudioClip>(ResourcePath.Audio.Man_BeAtk);
        }

        public void Interrupt()
        {
            
            playerState.BeBacking = false;
            animator.SetTrigger("beAtkEnd");
        }


        public void OnEnter(ref object[] o)
        {
            playerState.BeAttackInvincible = true;
            playerState.BeBacking = true;
            animator.SetTrigger("beAtk");
            audioSource.Play();
            time = 0;
            offSetDistance = (float)o[1];
            offSetTime = (float)o[2];
            offSetDir = (Vector3)o[3];
            offSetSpeed = offSetDistance / offSetTime;
        }

        public void OnFixedUpdate()
        {
            if (time < offSetTime)
            {
                time += Time.fixedDeltaTime;
                playerBody.transform.Translate(offSetDir * offSetSpeed * Time.fixedDeltaTime, Space.World);
            }
            else
            {
                Interrupt();
                Timer.TimerStart(playerData.InjuredInterval, () =>
                {
                    playerState.BeAttackInvincible = false;
                });
            }
        }

        public void OnUpdate()
        {

        }
    }
}
